/**
 * Filename: ResetActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 3:58
 */
package com.github.postsanf.yinian.activity;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.entity.YNLoginResponse;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;

/**
 * 重置密码
 */
public class ResetActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private RelativeLayout rl_reset_root;
    private EditText et_reset_code;
    private ImageView iv_reset_delete;
    private Button btn_submit_reset;
    private String mPhone;
    private String mPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_reset);
        BaseApplication.getInstance().mActivitys.add(this);
        try {
            mPhone = getIntent().getExtras().getString(ACTIONs.aAddData, "");
        } catch (NullPointerException e) {
            mPhone = "";
        }
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.reset_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        rl_reset_root = (RelativeLayout) findViewById(R.id.reset_root);
        et_reset_code = (EditText) findViewById(R.id.et_reset_code);
        iv_reset_delete = (ImageView) findViewById(R.id.iv_reset_delete);
        btn_submit_reset = (Button) findViewById(R.id.btn_submit_reset);
        et_reset_code.setHint("请输入密码");
        et_reset_code.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        btn_submit_reset.setText("重置密码");
        iv_reset_delete.setOnClickListener(this);
        btn_submit_reset.setOnClickListener(this);
        rl_reset_root.setOnClickListener(this);
        et_reset_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() <= 0) {
                    iv_reset_delete.setVisibility(View.GONE);
                } else {
                    iv_reset_delete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset_root:
                DisplayUtils.setFoucs(rl_reset_root);
                break;
            // 重置密码
            case R.id.btn_submit_reset:
                doReset();
                break;
            case R.id.iv_reset_delete:
                et_reset_code.setText("");
                iv_reset_delete.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    //检查信息格式
    private boolean checkInfo() {
        mPsw = et_reset_code.getText().toString().trim();
        if (mPsw.length() <= 6) {
            showToast("密码最少6位");
            return false;
        }
        return true;
    }

    private void doReset() {
        if (checkInfo()) {
            LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
            mParam.put("phonenumber", mPhone);
            mParam.put("password", mPsw);
            StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynResetPsw)).setMethod(HttpMethods.Post).setParamMap(mParam)
                    .setHttpListener(new HttpListener<String>(true, false, true) {
                        @Override
                        public void onSuccess(String s, Response<String> response) {
                            YNLoginResponse resReset = gson.fromJson(s, YNLoginResponse.class);
                            if (ReqUtils.isSuccess(resReset.getCode())) {
//                                SharedPreferences.Editor ed = application.getSp_login().edit();
//                                ed.putString(getString(R.string.SP_YNS_USERNAME), YNDoSecretUtils.doSecretStr(mPhone));
//                                ed.putString(getString(R.string.SP_YNS_PASSWORD), YNDoSecretUtils.doSecretStr(mPsw));
//                                ed.putString(getString(R.string.LID), YNDoSecretUtils.doSecretStr(String.valueOf(resReset.getData().get(0).getUserid())));
//                                ed.commit();
//                                application.setCurID(resReset.getData().get(0).getUserid());
//                                application.setCurUser((resReset.getData().get(0)));
                                // 跳转至主页并关闭当前页面
//                                intent2Activity(MainActivity.class);
                                showToast("重置成功");
                                finish();
                            } else {
                                showToast(resReset.getMsg());
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, Response<String> response) {
                            showToast(CommonConstants.YN_NET_LINK);
                        }
                    });
            liteHttp.executeAsync(req);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
                btn_submit_reset.setTextColor(mResource.getColorStateList(mResource.getIdentifier("txtcolor_white2theme", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                btn_submit_reset.setBackground(mResource.getDrawable(mResource.getIdentifier("btn_invite_bg_sel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
